package temple.model.applicationModule;

import oracle.jbo.JboException;
import oracle.jbo.Key;
import oracle.jbo.RowIterator;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.server.*;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.EntityDefImpl;
import oracle.jbo.server.EntityImpl;


import oracle.jbo.server.ViewLinkImpl;
import oracle.jbo.server.ViewObjectImpl;

import temple.model.applicationModule.common.AppModuleAM;
import temple.model.entity.TempleUserEOImpl;
import temple.model.view.TempleUserVOImpl;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Mon Jul 02 23:28:35 PDT 2012
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class AppModuleAMImpl extends ApplicationModuleImpl implements AppModuleAM {
    /**
     * This is the default constructor (do not remove).
     */
    public AppModuleAMImpl() {
    }

    /**
     * Container's getter for StateCode1.
     * @return StateCode1
     */
    public ViewObjectImpl getStateCode1() {
        return (ViewObjectImpl)findViewObject("StateCode1");
    }

    /**
     * Container's getter for TempleUserR1.
     * @return TempleUserR1
     */
    public ViewObjectImpl getTempleUserR1() {
        return (ViewObjectImpl)findViewObject("TempleUserR1");
    }

    /**
     * Container's getter for YesNoS1.
     * @return YesNoS1
     */
    public ViewObjectImpl getYesNoS1() {
        return (ViewObjectImpl)findViewObject("YesNoS1");
    }

    /**
     * Container's getter for TempleUser1.
     * @return TempleUser1
     */
    public TempleUserVOImpl getTempleUser1() {
        return (TempleUserVOImpl)findViewObject("TempleUser1");
    }

    /**
     * Container's getter for UserService1.
     * @return UserService1
     */
    public ViewObjectImpl getUserService1() {
        return (ViewObjectImpl)findViewObject("UserService1");
    }

    /**
     * Container's getter for UserPass1.
     * @return UserPass1
     */
    public ViewObjectImpl getUserPass1() {
        return (ViewObjectImpl)findViewObject("UserPass1");
    }

    /**
     * Container's getter for TempleServices1.
     * @return TempleServices1
     */
    public ViewObjectImpl getTempleServices1() {
        return (ViewObjectImpl)findViewObject("TempleServices1");
    }
    
    // method to check if user exists or not 
    public String findusereo(Integer userid){
        System.err.println("Find User Started");
        EntityDefImpl tuDef = TempleUserEOImpl.getDefinitionObject();
        Key uidKey = TempleUserEOImpl.createPrimaryKey(userid);
        EntityImpl tuimpl = tuDef.findByPrimaryKey(getDBTransaction(), uidKey);
        if (tuimpl != null ) {
            return "User Name is : " +tuimpl.getAttribute("UserName");
        }
        else
            return "No User Found";
    }
    // method 
    public TempleUserEOImpl finduserimpl(Integer userid){
        EntityDefImpl tudef = TempleUserEOImpl.getDefinitionObject();
        Key uidKey = TempleUserEOImpl.createPrimaryKey(userid);
        System.err.println("Key created is : "+uidKey);
        TempleUserEOImpl tuimpl = (TempleUserEOImpl)tudef.findByPrimaryKey(getDBTransaction(), uidKey);
        return tuimpl;
    }
    
    public String updatecity (Integer userid, String city){
        TempleUserEOImpl tuimpl = finduserimpl(userid);
        if (tuimpl != null){
            System.err.println(" Current City is : " +tuimpl.getCity());
            tuimpl.setCity(city);
            System.err.println("New city for User Set to " +tuimpl.getCity());
            try{
                getDBTransaction().commit();
            }
            catch(JboException ex){
                getDBTransaction().rollback();
                throw ex;
            }
            System.err.println("City for : "+tuimpl.getUserName() +"Changed To : " +tuimpl.getCity());
            return "City for : "+tuimpl.getUserName() +" Changed To : " +tuimpl.getCity();
        }
          else
            return " No User Found";
        }
    
    public String deleteUser (Integer userid){
        TempleUserEOImpl tuimpl = finduserimpl(userid);
        if (tuimpl != null){
            System.err.println("User Found");
            tuimpl.remove();
            try{
                getDBTransaction().commit();
            }
            catch(JboException ex){
                getDBTransaction().rollback();
                throw ex;
            }
            System.err.println("User : " +tuimpl.getUserId() +" deleted"); 
            return "User : " +tuimpl.getUserName() +" Is removed from DataBase";
        }
        else
            return "No User Found";
    }
    public String listUserServices (Integer userid){
        TempleUserEOImpl tuimpl = finduserimpl(userid);
        if (tuimpl != null){
            System.err.println("User Found");
           RowIterator usitr = tuimpl.getUserServiceEO();
           System.err.println("Entering Loop");
           int i = 0;
          while(usitr.hasNext()){
               while(i<1){
                   System.err.println( tuimpl.getUserName() +" used the following services : ");  
                   i++;
               }
             System.err.println(usitr.next().getAttribute("ServiceCode"));
           }
           if (i < 1){
               System.err.println("User did not use any of the Temple Services");
           }
           return "Services Printed";
        }
        else
            return " User Not Found";
    }
   
   
    public String printCityUsers(String city){
        ViewObject tuvo = this.findViewObject("TempleUser1");
        ViewCriteria tuvc = tuvo.createViewCriteria();
        ViewCriteriaRow tuvcr = tuvc.createViewCriteriaRow();
        tuvcr.setAttribute("City",city);
        tuvc.addElement(tuvcr);
        tuvo.applyViewCriteria(tuvc);
        tuvo.executeQuery();
        int i = 0;
        while(tuvo.hasNext()){
            while(i<1){
            System.err.println("Users Currently Living in  : " +tuvo.getCurrentRow().getAttribute("City"));
                i++;
            }
            System.err.println("User Name  : " +tuvo.next().getAttribute("UserName"));
        }
       if(i>0)
             return "Users Printed";
        else
            return "No User Found in : " +tuvcr.getAttribute("City");
    }
    /**
     * Container's getter for TempleUser2.
     * @return TempleUser2
     */
    public TempleUserVOImpl getTempleUser2() {
        return (TempleUserVOImpl)findViewObject("TempleUser2");
    }

    /**
     * Container's getter for UserService2.
     * @return UserService2
     */
    public ViewObjectImpl getUserService2() {
        return (ViewObjectImpl)findViewObject("UserService2");
    }

    /**
     * Container's getter for TempleUserEOtoUserServicesEOVL.
     * @return TempleUserEOtoUserServicesEOVL
     */
    public ViewLinkImpl getTempleUserEOtoUserServicesEOVL() {
        return (ViewLinkImpl)findViewLink("TempleUserEOtoUserServicesEOVL");
    }
}
